<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>selenium系列01-定位方式 | 莫伊の测试笔记</title><meta name="keywords" content="Selenium,UI自动化"><meta name="author" content="莫伊"><meta name="copyright" content="莫伊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="以我们万能的百度页面为例来展开！  ID12# 找到搜索框inputele &#x3D; driver.find_element_by_id(&quot;kw&quot;) ⏰ ATTENTION：一般来说，ID是唯一的（但是不排除这种情况——开发写代码的时候不规范🤣） CLASS12345678# 通过class查找元素，返回匹配到的第一个元素# 搜索框inputele &#x3D; driver.find_e">
<meta property="og:type" content="article">
<meta property="og:title" content="selenium系列01-定位方式">
<meta property="og:url" content="https://mooii.gitee.io/2021/12/14/selenium01/index.html">
<meta property="og:site_name" content="莫伊の测试笔记">
<meta property="og:description" content="以我们万能的百度页面为例来展开！  ID12# 找到搜索框inputele &#x3D; driver.find_element_by_id(&quot;kw&quot;) ⏰ ATTENTION：一般来说，ID是唯一的（但是不排除这种情况——开发写代码的时候不规范🤣） CLASS12345678# 通过class查找元素，返回匹配到的第一个元素# 搜索框inputele &#x3D; driver.find_e">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5372(20211213-233028).PNG">
<meta property="article:published_time" content="2021-12-14T15:23:14.000Z">
<meta property="article:modified_time" content="2021-12-19T14:20:06.088Z">
<meta property="article:author" content="莫伊">
<meta property="article:tag" content="Selenium">
<meta property="article:tag" content="UI自动化">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5372(20211213-233028).PNG"><link rel="shortcut icon" href="https://mooii.oss-cn-shanghai.aliyuncs.com/%E3%83%AA%E3%82%B9%E3%81%AE%E3%82%A2%E3%82%A4%E3%82%B3%E3%83%B3%E3%81%A7%E3%81%99%E3%81%9F%E3%81%84%E3%80%82.png"><link rel="canonical" href="https://mooii.gitee.io/2021/12/14/selenium01/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'selenium系列01-定位方式',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-12-19 22:20:06'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiper.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiperstyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-clock/lib/clock.min.css" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><script async src="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus@latest/lib/carousel-touch.min.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://portrait.gitee.com/uploads/avatars/user/1627/4881517_lolien_1621782146.png!avatar100" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5372(20211213-233028).PNG')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">莫伊の测试笔记</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">selenium系列01-定位方式</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-12-14T15:23:14.000Z" title="发表于 2021-12-14 23:23:14">2021-12-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-12-19T14:20:06.088Z" title="更新于 2021-12-19 22:20:06">2021-12-19</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Selenium/">Selenium</a></span></div><div class="meta-secondline"></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>以我们万能的百度页面为例来展开！</p>
</blockquote>
<h2 id="ID"><a href="#ID" class="headerlink" title="ID"></a>ID</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 找到搜索框</span></span><br><span class="line">inputele = driver.find_element_by_id(<span class="string">&quot;kw&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>⏰ <em>ATTENTION</em>：一般来说，ID是唯一的（但是不排除这种情况——开发写代码的时候不规范🤣）</p>
<h2 id="CLASS"><a href="#CLASS" class="headerlink" title="CLASS"></a>CLASS</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过class查找元素，返回匹配到的第一个元素</span></span><br><span class="line"><span class="comment"># 搜索框</span></span><br><span class="line">inputele = driver.find_element_by_class_name(<span class="string">&quot;s_ipt&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 假如页面上存在多个class为s_ipt的元素则可用以下方式</span></span><br><span class="line">input_list = driver.find_elements_by_class_name(<span class="string">&quot;s_ipt&quot;</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> input_list:</span><br><span class="line">    <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure>
<p>⏰ <em>ATTENTION</em>：</p>
<ul>
<li>一般而言，class不唯一</li>
<li><p><code>find_element_by_class_name</code> 只返回匹配到的<strong>第一个</strong>class元素，找不到元素会报错</p>
</li>
<li><p><code>find_elements_by_class_name</code> 返回<strong>所有</strong>匹配到的元素列表，找不到元素会返回一个空列表</p>
</li>
</ul>
<h2 id="NAME"><a href="#NAME" class="headerlink" title="NAME"></a>NAME</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过name查找元素</span></span><br><span class="line"><span class="comment"># 搜索框</span></span><br><span class="line">inputele = driver.find_element_by_name(<span class="string">&quot;wd&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 若页面存在多个name相同的元素则可用以下方式</span></span><br><span class="line">input_list = driver.find_elements_by_name(<span class="string">&quot;wd&quot;</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> input_list:</span><br><span class="line">    <span class="built_in">print</span>(i)</span><br></pre></td></tr></table></figure>
<p>⏰ <em>ATTENTION</em>：</p>
<ul>
<li>name方式与class定位方式类似，不赘述</li>
</ul>
<h2 id="LINK-TEXT"><a href="#LINK-TEXT" class="headerlink" title="LINK_TEXT"></a>LINK_TEXT</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 用于定位文本链接</span></span><br><span class="line"><span class="comment"># 例如定位百度页面的新闻链接</span></span><br><span class="line">driver.find_element_by_link_text(<span class="string">&#x27;新闻&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>⏰ <em>ATTENTION</em>：</p>
<ul>
<li>用于定位文本链接</li>
<li>相当于超链接文本完全匹配</li>
</ul>
<h2 id="PARTIAL-LINK-TEXT"><a href="#PARTIAL-LINK-TEXT" class="headerlink" title="PARTIAL_LINK_TEXT"></a>PARTIAL_LINK_TEXT</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过partial_link定位&quot;新闻&quot;这个链接并点击</span></span><br><span class="line">driver.find_element_by_partial_link_text(<span class="string">&#x27;闻&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>⏰ <em>ATTENTION</em>：</p>
<ul>
<li>超链接很长时，可用partial_link定位方式</li>
<li>相当于超链接文本模糊匹配</li>
</ul>
<h2 id="XPATH"><a href="#XPATH" class="headerlink" title="XPATH"></a>XPATH</h2><p>xpath全称是XML Path Language，用于解析html与xml，由于xpath定位方式是从头到尾遍历页面，所以速度较慢，下面介绍一下xpath比较常用的一些定位方式</p>
<p>那么在了解xpaht之前，我们先看一下xpath常见表达式以及通配符的含义</p>
<p>通配符：</p>
<ul>
<li><code>nodename</code> 选取此节点的所有子节点 </li>
<li><p><code>/</code> 当前节点的<strong>子元素</strong> </p>
</li>
<li><p><code>//</code> 从匹配选择的当前节点选择文档中的节点，不考虑位置（从任何地方选取）；也可代表当前节点下的<strong>子孙元素</strong> </p>
</li>
<li><p><code>.</code> 选取当前节点 </p>
</li>
<li><p><code>..</code> 选取当前节点的父节点 </p>
</li>
<li><code>@</code> 选取属性 </li>
</ul>
<p>常用表达式：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 选取属于bookstore子元素的第一个book元素</span></span><br><span class="line">/bookstore/book[<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#选取属于bookstore子元素的最后一个book元素</span></span><br><span class="line">/bookstore/book[last()]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取属于bookstore子元素的倒数第二个book元素</span></span><br><span class="line">/bookstore/book[last()-<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取最前面的两个属于bookstore元素的子元素的book元素</span></span><br><span class="line">/bookstore/book[position()&lt;<span class="number">3</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取所有title元素，且这些元素拥有值为eng的lang元素</span></span><br><span class="line">//title[@lang=<span class="string">&#x27;eng&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取bookstore元素的所有book元素，且其中的price元素值大于35.5</span></span><br><span class="line">/bookstore/book[price&gt;<span class="number">35.5</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选取bookstore元素中的book元素的所有title元素，且其中的price元素值大于35.5</span></span><br><span class="line">/bookstore/book[price&gt;<span class="number">35.5</span>]/title</span><br></pre></td></tr></table></figure>
<p>了解了上述基本内容之后，我们来看下xpath几种常用的方法👇👇👇</p>
<h3 id="绝对路径定位"><a href="#绝对路径定位" class="headerlink" title="绝对路径定位"></a>绝对路径定位</h3><p>将xpath表达式从HTML的最外层节点，逐层填写，最后定位到操作元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 例如百度页面的“百度一下”按钮，绝对路径的xpath为：</span></span><br><span class="line">xpath = <span class="string">&quot;/html/body/div[1]/div[2]/div[5]/div[1]/div/form/span[2]/input&quot;</span></span><br><span class="line">driver.find_element_by_xpath(xpath)</span><br></pre></td></tr></table></figure>
<p>特点：一旦路径变化，会导致定位失败，不推荐</p>
<h3 id="相对路径定位"><a href="#相对路径定位" class="headerlink" title="相对路径定位"></a>相对路径定位</h3><p>给出元素路径的部分信息，在HTML的任意层次中寻找符合条件的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 例如百度页面的“百度一下”按钮，相对路径的xpath为：</span></span><br><span class="line">xpath = <span class="string">&#x27;//*[@id=&quot;su&quot;]&#x27;</span></span><br><span class="line">driver.find_element_by_xpath(xpath)</span><br></pre></td></tr></table></figure>
<p>特点：相对路径提取的是元素的部分特征，可结合其他的属性共同定位，方式较灵活</p>
<h3 id="索引定位"><a href="#索引定位" class="headerlink" title="索引定位"></a>索引定位</h3><p>语法：<code>//标签名[下标值]</code> ，索引的初始值为1</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第二个子元素的div元素</span></span><br><span class="line">xpath = <span class="string">&#x27;//div[2]&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 父元素为div的第二个子元素，不区分类型</span></span><br><span class="line">xpath = <span class="string">&#x27;//div/*[2]&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 某类型倒数第几个子元素</span></span><br><span class="line">xpath = <span class="string">&#x27;//div[last()]&#x27;</span>				<span class="comment"># 定位到div类型最后一个元素</span></span><br><span class="line">xpath = <span class="string">&#x27;//div[last()-1]&#x27;</span></span><br><span class="line">xpath = <span class="string">&#x27;//div[last()-n]&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 范围选择</span></span><br><span class="line">xpath = <span class="string">&#x27;//ul/li[position()&lt;=2]&#x27;</span>			<span class="comment"># 定位ul类型下前两个li元素</span></span><br><span class="line">xpath = <span class="string">&#x27;//ul/li[position()&gt;=last()-1]&#x27;</span>		<span class="comment"># 定位ul类型下的最后2个li元素</span></span><br></pre></td></tr></table></figure>
<h3 id="属性值定位"><a href="#属性值定位" class="headerlink" title="属性值定位"></a>属性值定位</h3><p>元素中比较常见的属性有id、name、class等，若没有这些常用的属性值，也可以查找元素中有其他唯一标识的属性，用<code>@</code> 选取元素</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 百度搜索输入框 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;s_ipt&quot;</span> <span class="attr">name</span>=<span class="string">&quot;wd&quot;</span> <span class="attr">id</span>=<span class="string">&quot;kw&quot;</span> <span class="attr">maxlength</span>=<span class="string">&quot;100&quot;</span> <span class="attr">autocomplete</span>=<span class="string">&quot;off&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 百度的输入框属性有class、name、id，可以通过属性值定位输入框</span></span><br><span class="line">xpath1 = <span class="string">&#x27;//*[@class=&quot;s_ipt&quot;]&#x27;</span></span><br><span class="line">xpath2 = <span class="string">&#x27;//*[@name=&quot;wd&quot;]&#x27;</span></span><br><span class="line">xpath3 = <span class="string">&#x27;//*[@id=&quot;kw&quot;]&#x27;</span></span><br></pre></td></tr></table></figure>
<h3 id="逻辑运算"><a href="#逻辑运算" class="headerlink" title="逻辑运算"></a>逻辑运算</h3><p>xpath支持逻辑运算符and与or</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 还是以百度搜索框为例 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;s_ipt&quot;</span> <span class="attr">name</span>=<span class="string">&quot;wd&quot;</span> <span class="attr">id</span>=<span class="string">&quot;kw&quot;</span> <span class="attr">maxlength</span>=<span class="string">&quot;100&quot;</span> <span class="attr">autocomplete</span>=<span class="string">&quot;off&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过逻辑运算符，可定位到输入框</span></span><br><span class="line">xpath1 = <span class="string">&#x27;//*[@name=&quot;wd&quot; and @id=&quot;kw&quot;]&#x27;</span></span><br></pre></td></tr></table></figure>
<p>特点：多个属性值联合定位，可以使定位更加准确；对于存在多个相同标签的元素，如果存在不同的属性值，也可以用这个方式区别开来</p>
<h3 id="属性值匹配"><a href="#属性值匹配" class="headerlink" title="属性值匹配"></a>属性值匹配</h3><p><code>start-with()</code> ：属性值以XX开头</p>
<p><code>ends-with()</code>：属性值以XX结尾</p>
<p><code>contains()</code> ：属性值包含</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 例如 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;s_ipt&quot;</span> <span class="attr">name</span>=<span class="string">&quot;wd ipt er&quot;</span> <span class="attr">id</span>=<span class="string">&quot;kw&quot;</span> <span class="attr">maxlength</span>=<span class="string">&quot;100&quot;</span> <span class="attr">autocomplete</span>=<span class="string">&quot;off&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">xpath1 = <span class="string">&#x27;//input[starts-with(@name,&quot;wd&quot;)]&#x27;</span></span><br><span class="line">xpath2 = <span class="string">&#x27;//input[ends-with(@id,&quot;er&quot;)]&#x27;</span></span><br><span class="line">xpath3 = <span class="string">&#x27;//input[contains(@id,&quot;ipt&quot;)]&#x27;</span></span><br><span class="line"><span class="comment"># 任意匹配</span></span><br><span class="line">xpath4 = <span class="string">&#x27;//input[@*=&quot;s_ipt&quot;]&#x27;</span></span><br></pre></td></tr></table></figure>
<p>特点：可以用来定位属性值不太规律/存在部分变动/属性中间有空格的情况</p>
<p>⏰ <em>ATTENTION</em>：</p>
<ul>
<li>如果属性值中间包含空格，Webdriver定位的时候容易出错，有时定位不到，可以使用属性值匹配的方式去定位元素</li>
<li>属性任意值匹配相当于模糊查询，只要目标元素中任意属性值匹配成功就能定位成功，但要注意这个元素值在页面中的唯一性</li>
</ul>
<p>那么以上，就是xpath常用的一些定位方式，以上这些，基本能解决工作中大部分的元素定位问题；当日还有其他的一些函数定位啦、轴定位啦等等，由于这些我本人在工作中比较少用，所以就没有写到这里面，感兴趣的盆友可以自行了解凹~</p>
<h2 id="CSS-SELECTOR"><a href="#CSS-SELECTOR" class="headerlink" title="CSS_SELECTOR"></a>CSS_SELECTOR</h2><p>老规矩，我们还是先来了解一下css_selector的基本语法</p>
<p><code>.</code> 代表class；<code>.intro</code> 选择class=”intro”的所有元素</p>
<p><code>#</code> 代表ID</p>
<p><code>*</code> 代表选择所有元素；</p>
<p><code>element</code> 代表选择所有element元素；<code>p</code> 选择所有元素</p>
<p><code>element1,element2</code>代表选择所有element1元素和所有element2元素；例如`div,p</p>
<p><code>element1 emelent2</code>代表选择element1内部的所有element2元素；例如`div p</p>
<p><code>element1&gt;element2</code>选择父元素为element1的所有element2元素；例如`div&gt;p</p>
<p><code>element1+element2</code>选择紧接在element1元素之后的所有element2元素；例如`div+p</p>
<h3 id="单一属性定位"><a href="#单一属性定位" class="headerlink" title="单一属性定位"></a>单一属性定位</h3><p>css_selector定位方式与xpath类似，也可通过元素的属性来定位</p>
<h4 id="通过类型"><a href="#通过类型" class="headerlink" title="通过类型"></a>通过类型</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过类型</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&#x27;input&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="通过ID"><a href="#通过ID" class="headerlink" title="通过ID"></a>通过ID</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过ID</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&#x27;#kw&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="通过class"><a href="#通过class" class="headerlink" title="通过class"></a>通过class</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 通过class</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&#x27;.s_ipt&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="其他属性"><a href="#其他属性" class="headerlink" title="其他属性"></a>其他属性</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 其他属性</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;[name=&#x27;wd&#x27;]&quot;</span>)</span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;[type=&#x27;text&#x27;]&quot;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="组合属性定位"><a href="#组合属性定位" class="headerlink" title="组合属性定位"></a>组合属性定位</h3><h4 id="ID组合属性"><a href="#ID组合属性" class="headerlink" title="ID组合属性"></a>ID组合属性</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># ID组合属性</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input#kw&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="CLASS组合属性"><a href="#CLASS组合属性" class="headerlink" title="CLASS组合属性"></a>CLASS组合属性</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># class组合属性</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input.s_ipt&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="其他属性组合"><a href="#其他属性组合" class="headerlink" title="其他属性组合"></a>其他属性组合</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 其他属性组合定位</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input[name=&#x27;wd&#x27;]&quot;</span>)</span><br><span class="line"><span class="comment"># 其他的两个属性组合</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;[name=&#x27;wd&#x27;][type=&#x27;text&#x27;]&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="模糊匹配属性值"><a href="#模糊匹配属性值" class="headerlink" title="模糊匹配属性值"></a>模糊匹配属性值</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 以百度首页搜索按钮为例 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;百度一下&quot;</span> <span class="attr">id</span>=<span class="string">&quot;su&quot;</span> <span class="attr">class</span>=<span class="string">&quot;btn self-btn bg s_btn&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><code>~=</code>属性值有空格，匹配其中一个</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input[class~=&#x27;btn&#x27;]&quot;</span>)</span><br></pre></td></tr></table></figure>
<p><code>^=</code>匹配属性值以XX开头</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">driver.find_element_by_css_selector(&quot;input[class^=&#x27;btn&#x27;]&quot;)</span><br></pre></td></tr></table></figure>
<p><code>&amp;=</code>匹配属性值以XX结尾</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input[class$=&#x27;btn&#x27;]&quot;</span>)</span><br></pre></td></tr></table></figure>
<p><code>*=</code>匹配包含XX属性的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input[*=&#x27;百度一下&#x27;]&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>匹配被-分隔的属性值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;input[class|=&#x27;s&#x27;]&quot;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="层级定位"><a href="#层级定位" class="headerlink" title="层级定位"></a>层级定位</h3><h4 id="element1-gt-element2"><a href="#element1-gt-element2" class="headerlink" title="element1&gt;element2"></a>element1&gt;element2</h4><p>element1下的element2元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定位百度首页的输入框</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;span#s_kw_wrap&gt;input&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="父子兄弟节点定位"><a href="#父子兄弟节点定位" class="headerlink" title="父子兄弟节点定位"></a>父子兄弟节点定位</h4><p><code>element:nth-child(n)</code>第几个element标签</p>
<p><code>element:nth-last-child(n)</code>倒数第几个element标签</p>
<p><code>element:first-child</code>第一个element标签</p>
<p><code>element:last-child</code>最后一个element标签</p>
<p><code>element:only-child</code>唯一的element标签</p>
<p><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/image.png" alt="image"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定位百度首页的新闻超链接</span></span><br><span class="line">driver.find_element_by_css_selector(<span class="string">&quot;#s-top-left&gt;a:nth-child(1)&quot;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><blockquote>
<p>推荐定位方式优先级</p>
</blockquote>
<ul>
<li>优先级最高：ID</li>
<li><p>优先级其次：name</p>
</li>
<li><p>优先级再次：CSS selector</p>
</li>
<li>优先级再次：Xpath</li>
</ul>
<blockquote>
<p>CSS优先于xpath的原因：</p>
</blockquote>
<ul>
<li>CSS配合HTML，原理是匹配对象；xpath配合XML，原理是便利，css的性能更加优秀</li>
<li><p>CSS语法比Xpath简介</p>
</li>
<li><p>前端用CSS</p>
</li>
</ul>
<blockquote>
<p>⏰ <em>ATTENTION</em>：</p>
</blockquote>
<ul>
<li>定位的时候，要找元素的唯一属性</li>
<li><p>如果元素没有唯一属性，可以先找到能被定位到具有唯一属性的父/子/兄弟等等的元素，再进行辅助定位</p>
</li>
<li><p>千万不用用随机的唯一属性定位！！！！o(╥﹏╥)o</p>
</li>
<li>最重要的是，跟开发沟通，让开发大佬们把元素加上唯一的name或者ID🤣🤣🤣</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>selenium系列01-定位方式</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="https://mooii.gitee.io/2021/12/14/selenium01/">https://mooii.gitee.io/2021/12/14/selenium01/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>莫伊</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2021-12-14</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2021-12-19</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Selenium/">Selenium</a><a class="post-meta__tags" href="/tags/UI%E8%87%AA%E5%8A%A8%E5%8C%96/">UI自动化</a></div><div class="post_share"><div class="social-share" data-image="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5372(20211213-233028).PNG" data-sites="weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://mooii.oss-cn-shanghai.aliyuncs.com/20211021204119.png" target="_blank"><img class="post-qr-code-img" src="https://mooii.oss-cn-shanghai.aliyuncs.com/20211021204119.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://mooii.oss-cn-shanghai.aliyuncs.com/20211021204141.png" target="_blank"><img class="post-qr-code-img" src="https://mooii.oss-cn-shanghai.aliyuncs.com/20211021204141.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/12/14/selenium02/"><img class="prev-cover" src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5373(20211213-233042).PNG" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">selenium系列02-控制浏览器常用操作</div></div></a></div><div class="next-post pull-right"><a href="/2021/12/13/pytest20/"><img class="next-cover" src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5375(20211213-233124).PNG" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">pytest20</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://portrait.gitee.com/uploads/avatars/user/1627/4881517_lolien_1621782146.png!avatar100" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">莫伊</div><div class="author-info__description">幸运交给锦鲤，你只管努力就好</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/mooii"><i class="https://mooii.oss-cn-shanghai.aliyuncs.com/20211021164956.png"></i><span>我的主页</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">人生有限，接受事与愿违！！！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#ID"><span class="toc-number">1.</span> <span class="toc-text">ID</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CLASS"><span class="toc-number">2.</span> <span class="toc-text">CLASS</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#NAME"><span class="toc-number">3.</span> <span class="toc-text">NAME</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#LINK-TEXT"><span class="toc-number">4.</span> <span class="toc-text">LINK_TEXT</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PARTIAL-LINK-TEXT"><span class="toc-number">5.</span> <span class="toc-text">PARTIAL_LINK_TEXT</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#XPATH"><span class="toc-number">6.</span> <span class="toc-text">XPATH</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%9D%E5%AF%B9%E8%B7%AF%E5%BE%84%E5%AE%9A%E4%BD%8D"><span class="toc-number">6.1.</span> <span class="toc-text">绝对路径定位</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%AF%B9%E8%B7%AF%E5%BE%84%E5%AE%9A%E4%BD%8D"><span class="toc-number">6.2.</span> <span class="toc-text">相对路径定位</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%E5%AE%9A%E4%BD%8D"><span class="toc-number">6.3.</span> <span class="toc-text">索引定位</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E5%80%BC%E5%AE%9A%E4%BD%8D"><span class="toc-number">6.4.</span> <span class="toc-text">属性值定位</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97"><span class="toc-number">6.5.</span> <span class="toc-text">逻辑运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E5%80%BC%E5%8C%B9%E9%85%8D"><span class="toc-number">6.6.</span> <span class="toc-text">属性值匹配</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS-SELECTOR"><span class="toc-number">7.</span> <span class="toc-text">CSS_SELECTOR</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%95%E4%B8%80%E5%B1%9E%E6%80%A7%E5%AE%9A%E4%BD%8D"><span class="toc-number">7.1.</span> <span class="toc-text">单一属性定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E7%B1%BB%E5%9E%8B"><span class="toc-number">7.1.1.</span> <span class="toc-text">通过类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87ID"><span class="toc-number">7.1.2.</span> <span class="toc-text">通过ID</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87class"><span class="toc-number">7.1.3.</span> <span class="toc-text">通过class</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E5%B1%9E%E6%80%A7"><span class="toc-number">7.1.4.</span> <span class="toc-text">其他属性</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%84%E5%90%88%E5%B1%9E%E6%80%A7%E5%AE%9A%E4%BD%8D"><span class="toc-number">7.2.</span> <span class="toc-text">组合属性定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#ID%E7%BB%84%E5%90%88%E5%B1%9E%E6%80%A7"><span class="toc-number">7.2.1.</span> <span class="toc-text">ID组合属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CLASS%E7%BB%84%E5%90%88%E5%B1%9E%E6%80%A7"><span class="toc-number">7.2.2.</span> <span class="toc-text">CLASS组合属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E5%B1%9E%E6%80%A7%E7%BB%84%E5%90%88"><span class="toc-number">7.2.3.</span> <span class="toc-text">其他属性组合</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A8%A1%E7%B3%8A%E5%8C%B9%E9%85%8D%E5%B1%9E%E6%80%A7%E5%80%BC"><span class="toc-number">7.2.4.</span> <span class="toc-text">模糊匹配属性值</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E7%BA%A7%E5%AE%9A%E4%BD%8D"><span class="toc-number">7.3.</span> <span class="toc-text">层级定位</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#element1-gt-element2"><span class="toc-number">7.3.1.</span> <span class="toc-text">element1&gt;element2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%88%B6%E5%AD%90%E5%85%84%E5%BC%9F%E8%8A%82%E7%82%B9%E5%AE%9A%E4%BD%8D"><span class="toc-number">7.3.2.</span> <span class="toc-text">父子兄弟节点定位</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">8.</span> <span class="toc-text">总结</span></a></li></ol></div></div><div class="card-widget card-recommend-post"><div class="item-headline"><i class="fas fa-dharmachakra"></i><span>相关推荐</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/12/14/selenium02/" title="selenium系列02-控制浏览器常用操作"><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5373(20211213-233042).PNG" alt="selenium系列02-控制浏览器常用操作"></a><div class="content"><a class="title" href="/2021/12/14/selenium02/" title="selenium系列02-控制浏览器常用操作">selenium系列02-控制浏览器常用操作</a><time datetime="2021-12-14" title="发表于 2021-12-14">2021-12-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/15/selenium03/" title="selenium系列03-web控件交互"><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5372(20211213-233028).PNG" alt="selenium系列03-web控件交互"></a><div class="content"><a class="title" href="/2021/12/15/selenium03/" title="selenium系列03-web控件交互">selenium系列03-web控件交互</a><time datetime="2021-12-15" title="发表于 2021-12-15">2021-12-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/15/selenium04/" title="selenium系列04-隐式等待与显示等待"><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5371(20211213-233011).PNG" alt="selenium系列04-隐式等待与显示等待"></a><div class="content"><a class="title" href="/2021/12/15/selenium04/" title="selenium系列04-隐式等待与显示等待">selenium系列04-隐式等待与显示等待</a><time datetime="2021-12-15" title="发表于 2021-12-15">2021-12-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/16/selenium05/" title="selenium系列05-js处理方式"><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5375(20211213-233124).PNG" alt="selenium系列05-js处理方式"></a><div class="content"><a class="title" href="/2021/12/16/selenium05/" title="selenium系列05-js处理方式">selenium系列05-js处理方式</a><time datetime="2021-12-16" title="发表于 2021-12-16">2021-12-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/16/selenium06/" title="selenium系列06-文件上传及弹框处理"><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5373(20211213-233042).PNG" alt="selenium系列06-文件上传及弹框处理"></a><div class="content"><a class="title" href="/2021/12/16/selenium06/" title="selenium系列06-文件上传及弹框处理">selenium系列06-文件上传及弹框处理</a><time datetime="2021-12-16" title="发表于 2021-12-16">2021-12-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/17/selenium07/" title="selenium系列07-PO处理模式"><img src="https://mooii.oss-cn-shanghai.aliyuncs.com/IMG_5375(20211213-233124).PNG" alt="selenium系列07-PO处理模式"></a><div class="content"><a class="title" href="/2021/12/17/selenium07/" title="selenium系列07-PO处理模式">selenium系列07-PO处理模式</a><time datetime="2021-12-17" title="发表于 2021-12-17">2021-12-17</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2022 By 莫伊</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">好好学习🧡天天学习</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><div class="js-pjax"></div><script data-pjax defer src="/js/custom/fixed_card_widget.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><div id="fixedcard-dashboard"><button class="fixedcard-activebtn" type="button" title="用户信息" onclick="FixedCardWidget(&quot;class&quot;,&quot;card-info&quot;,&quot;0&quot;)"><i class="fas fa-address-book"></i></button><button class="fixedcard-activebtn" type="button" title="电子钟" onclick="FixedCardWidget(&quot;class&quot;,&quot;card-clock&quot;,&quot;0&quot;)"><i class="fas fa-cloud-sun"></i></button><button class="fixedcard-activebtn" type="button" title="相关推荐" onclick="FixedCardWidget(&quot;class&quot;,&quot;card-recommend-post&quot;,&quot;0&quot;)"><i class="fas fa-dharmachakra"></i></button><div class="fixedcard-user-avatar fixedcard-activebtn" onclick="RemoveFixedCardWidget()"><img class="fixedcard-user-avatar-img" src="https://portrait.gitee.com/uploads/avatars/user/1627/4881517_lolien_1621782146.png!avatar100" title="莫伊"></div></div></div><!-- hexo injector body_end start --> <script data-pjax>if(document.getElementById('recent-posts') && location.pathname =='/'){
    
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms"></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载swiper')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script>
<script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiper.min.js"></script>
<script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper@0.18/swiper/swiperindex.js"></script>
<style></style><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = 'undefined';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><script data-pjax>
  function butterfly_clock_injector_config(){
    var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
    var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img class="entered loading" id="card-clock-loading" src="https://npm.elemecdn.com/hexo-butterfly-clock/lib/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading"/></div></div></div></div></div>';
    console.log('已挂载butterfly_clock')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_clock_injector_config();
  }
  else if (epage === cpage){
    butterfly_clock_injector_config();
  }
  </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax src="https://npm.elemecdn.com/hexo-butterfly-clock/lib/clock.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>